package org.jeecg.modules.demo.jxcx.controller;



import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.demo.ddyht.controller.DdygPiciController;
import org.jeecg.modules.demo.ddyht.entity.DdygInventory;
import org.jeecg.modules.demo.ddyht.entity.DdygPici;
import org.jeecg.modules.demo.ddyht.service.IDdygInventoryService;
import org.jeecg.modules.demo.ddyht.service.IDdygPiciService;
import org.jeecg.modules.demo.jxcx.Utils.R;
import org.jeecg.modules.demo.jxcx.Utils.RandomUtil;
import org.jeecg.modules.demo.jxcx.dto.model;
import org.jeecg.modules.demo.jxcx.dto.traner;
import org.jeecg.modules.demo.jxcx.po.*;
import org.jeecg.modules.demo.jxcx.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * (JxcPici)表控制层
 *
 * @author makejava
 * @since 2024-08-05 14:35:13
 */
@RestController
@RequestMapping("jxcPici")
public class JxcPiciController{
    private static Logger logger = LoggerFactory.getLogger(JxcPiciController.class);
    /**
     * 服务对象
     */
    @Resource
    private JxcPiciService jxcPiciService;
    @Resource
    private JxcSaleService jxcSaleService;
    @Resource
    private JxcStockService jxcStockService;
    @Resource
    private JxcSupplierService jxcSupplierService;
    @Resource
    private JxcCustomService jxcCustomService;
    @Resource
    private JxcCzrzService jxcCzrzService;
    @Resource
    private JxcInventoryService jxcInventoryService;
    @Resource
    private IDdygInventoryService ddygInventoryService;
    @Resource
    private IDdygPiciService ddygPiciService;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param jxcPici 查询实体
     * @return 所有数据
     */
    @GetMapping
    public R selectAll(Page<JxcPici> page, JxcPici jxcPici) {
        return new R().ok(this.jxcPiciService.page(page, new QueryWrapper<>(jxcPici)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        return new R().ok(this.jxcPiciService.getById(id));
    }

    @PostMapping("/queryall")
    @ResponseBody
    public R queryall(@RequestBody JSONObject params) {
        String companyId = params.getString("companyId");
        String id = params.getString("id");
        String orstate = params.getString("orstate");
        return new R().ok(jxcPiciService.getByall(companyId, id, orstate));
    }
    @PostMapping("/modelall")
    @ResponseBody
    public Result queryallmodel(@RequestBody JSONObject params) {
        String companyId = params.getString("companyId");
//        List<model> getmodelall = jxcPiciService.getmodelall(companyId);
        List<DdygInventory> getByCompanyId = ddygInventoryService.getBycompany(companyId);
        JSONArray jsonArray = new JSONArray();
        for (int i = 0; i < getByCompanyId.size(); i++) {
            JSONObject json = new JSONObject();
            DdygInventory ddygInventory = getByCompanyId.get(i);
            json.put("型号", ddygInventory.getModel());
            json.put("库存数量:", ddygInventory.getNumber());
            jsonArray.add(json);
        }
        return Result.ok(jsonArray);
    }
    @PostMapping("/modelall2")
    @ResponseBody
    public Result queryallmodel2(@RequestBody JSONObject params) {
        String companyId = params.getString("companyId");
//        List<model> getmodelall = jxcPiciService.getmodelall(companyId);
        List<DdygPici> getByCompanyId = ddygPiciService.getBycompanyStock(companyId, "1");
        JSONArray jsonArray = new JSONArray();
        Set<String> seenUnitPrices = new HashSet<>();
        for (int i = 0; i < getByCompanyId.size(); i++) {
            JSONObject json = new JSONObject();
            DdygPici ddygPici = getByCompanyId.get(i);
			 if (!seenUnitPrices.contains(ddygPici.getModel())) {
                 DdygInventory ddygInventory = ddygInventoryService.getBymodelcompany(ddygPici.getModel(), companyId);
                 if (ddygInventory != null) {
                     json.put("型号", ddygPici.getModel());
                     json.put("库存数量", ddygInventory.getNumber());

//            json.put("库存数量:", ddygPici.getNumber());
                     jsonArray.add(json);
                     seenUnitPrices.add(ddygPici.getModel()); // 添加到集合中，表示已处理
                 }
             }
        }
        return Result.ok(jsonArray);
    }
    @PostMapping("/modelall3")
    @ResponseBody
    public Result queryallmodel3(@RequestBody JSONObject params) {
        String companyId = params.getString("companyId");
//        List<model> getmodelall = jxcPiciService.getmodelall(companyId);
        List<DdygPici> getByCompanyId = ddygPiciService.getBycompanyStock(companyId, "2");
        JSONArray jsonArray = new JSONArray();
        Set<String> seenUnitPrices = new HashSet<>();
        for (int i = 0; i < getByCompanyId.size(); i++) {
            JSONObject json = new JSONObject();
            DdygPici ddygPici = getByCompanyId.get(i);
			 if (!seenUnitPrices.contains(ddygPici.getModel())) {
                 json.put("型号", ddygPici.getModel());
//            json.put("库存数量:", ddygPici.getNumber());
                 jsonArray.add(json);
                 seenUnitPrices.add(ddygPici.getModel()); // 添加到集合中，表示已处理
             }
        }
        return Result.ok(jsonArray);
    }
    @PostMapping("/tranerall")
    @ResponseBody
    public Result queryalltraner(@RequestBody JSONObject params) {
        logger.info("数据查询开始{}", params);
        String companyId = params.getString("companyId");
        List<traner> gettranerall = jxcPiciService.gettranerall(companyId);
        logger.info("数据查询结束{}", gettranerall);
        return Result.OK(gettranerall);
    }
    @PostMapping("/likeall")
    @ResponseBody
    public R likeall(@RequestBody JSONObject params) {
        String like = params.getString("like");
        String companyId = params.getString("companyId");
        String id = params.getString("id");
        String orstate = params.getString("orstate");
        return new R().ok(jxcPiciService.getBylike(like, companyId, orstate, id));
    }
    /**
     * 新增数据
     *
     * @param jxcPici 实体对象
     * @return 新增结果
     */
    @PostMapping("add")
    public R insert(@RequestBody List<JxcPici> jxcPici) {
        logger.info("数据新增开始"+jxcPici.toString());
        BigDecimal allamount = new BigDecimal("0.00");
        LocalDateTime now = LocalDateTime.now();
        // 定义日期时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        String formattedDateTime = now.format(formatter);
        String only1 = RandomUtil.generateRandomString();
        String id = now.format(formatter2) + only1;
        String CompanyId = jxcPici.get(0).getCompanyId();
        String Traner = jxcPici.get(0).getTraner().trim();
        String IsPay = jxcPici.get(0).getIsPay();
        String PaymentMethod = jxcPici.get(0).getPaymentMethod();
//        String model = jxcPici.get(0).getModel();
        String orstate = jxcPici.get(0).getOrstate();
        List<String> modellist = new ArrayList<>();
        for (int i = 0; i < jxcPici.size(); i++) {
            modellist.add(jxcPici.get(i).getModel());
//
//            String model = jxcPici.get(i).getModel();
//            String number = jxcPici.get(i).getNumber();
//            String unitPrice = jxcPici.get(i).getUnitPrice();
//            JxcInventory jxcInventory = jxcInventoryService.getBymodel(model,CompanyId);
//            if (orstate.equals("1")){
//                // 进货
//                if (jxcInventory == null){
//                    jxcInventory = new JxcInventory();
//                    jxcInventory.setModel(model);
//                    jxcInventory.setNumber(number);
//                    jxcInventory.setUnitPrice(unitPrice);
//                    jxcInventory.setCompanyId(CompanyId);
//                    jxcInventory.setCreateTime(formattedDateTime);
//                    jxcInventoryService.save(jxcInventory);
//                }else{
//                    // 更新库存
//                    jxcInventory.setNumber(String.valueOf(new BigDecimal(jxcInventory.getNumber()).add(new BigDecimal(number))));
//                    jxcInventoryService.updateById(jxcInventory);
//                }
//            }else {
//                // 销货
//                if (jxcInventory == null){
//                    jxcInventory = new JxcInventory();
//                    jxcInventory.setId(id);
//                    jxcInventory.setModel(model);
//                    jxcInventory.setNumber("-"+number);
//                    jxcInventory.setUnitPrice(unitPrice);
//                    jxcInventory.setCompanyId(CompanyId);
//                    jxcInventory.setCreateTime(formattedDateTime);
//                    jxcInventoryService.save(jxcInventory);
//                }else{
//                    // 更新库存
//                    jxcInventory.setNumber(String.valueOf(new BigDecimal(jxcInventory.getNumber()).subtract(new BigDecimal(number))));
//                    jxcInventoryService.updateById(jxcInventory);
//                }
//            }
        }
        for (int i = 0; i < jxcPici.size(); i++) {
            String number = jxcPici.get(i).getNumber();
            String unitPrice = jxcPici.get(i).getUnitPrice();
            //用bigdecimal计算总额，小数点位数为0
            String totalfee = String.valueOf(new BigDecimal(number).multiply(new BigDecimal(unitPrice)));
            jxcPici.get(i).setAmount(totalfee);
            jxcPici.get(i).setId(id);
            //生成8位随机整数
            String only = RandomUtil.generateRandomString();

            jxcPici.get(i).setOnlyid(only+id);
            this.jxcPiciService.save(jxcPici.get(i));

            JxcCzrz jxcCzrz = new JxcCzrz();
            jxcCzrz.setId(DdygPiciController.setczrzid(id));
            jxcCzrz.setCompanyId(CompanyId);
            jxcCzrz.setInfor("型号:"+jxcPici.get(i).getModel()+"数量:"+jxcPici.get(i).getNumber()+"单价:"+jxcPici.get(i).getUnitPrice()+"金额:"+totalfee+"交易方:"+Traner);
            if (orstate.equals("1")){
                jxcCzrz.setType("进货批次新增");
                jxcCzrz.setOrstate("进货");
            }else{
                jxcCzrz.setType("销货批次新增");
                jxcCzrz.setOrstate("销货");
            }

            R add = jxcCzrzService.add(jxcCzrz);
            allamount = allamount.add(new BigDecimal(totalfee));
        }

        if (orstate.equals("1")){
            //进货
            JxcStock jxcStock = new JxcStock();
            jxcStock.setAmount(String.valueOf(allamount));
            jxcStock.setCompanyId(CompanyId);
            jxcStock.setCreateTime(formattedDateTime);
            jxcStock.setId(id);
            jxcStock.setPaymentMethod(PaymentMethod);
            jxcStock.setSeller(Traner);
            jxcStock.setIsPay(IsPay);
            jxcStock.setModel(modellist.toString());
            this.Stockadd(jxcStock,formattedDateTime,id);
            JxcCzrz jxcCzrz1 = new JxcCzrz();
            jxcCzrz1.setId(DdygPiciController.setczrzid(id));
            jxcCzrz1.setCompanyId(CompanyId);
            jxcCzrz1.setInfor("交易方:"+Traner+"金额:"+String.valueOf(allamount));
            jxcCzrz1.setType("进货单新增");
            R add = jxcCzrzService.add(jxcCzrz1);
            logger.info("数据新增结束-进货"+jxcStock.toString());
            return new R().ok(jxcStockService.save(jxcStock));
        }else{
            //销售
            JxcSale jxcSale = new JxcSale();
            jxcSale.setAmount(String.valueOf(allamount));
            jxcSale.setBuyer(Traner);
            jxcSale.setCompanyId(CompanyId);
            jxcSale.setCreateTime(formattedDateTime);
            jxcSale.setId(id);
            jxcSale.setIsPay(IsPay);
            jxcSale.setPaymentMethod(PaymentMethod);
            jxcSale.setModel(modellist.toString());
            this.Saleadd(jxcSale,formattedDateTime,id);
            JxcCzrz jxcCzrz1 = new JxcCzrz();
            jxcCzrz1.setId(DdygPiciController.setczrzid(id));
            jxcCzrz1.setCompanyId(CompanyId);
            jxcCzrz1.setInfor("交易方:"+Traner+"金额:"+String.valueOf(allamount));
            jxcCzrz1.setType("销货单新增");
            R add = jxcCzrzService.add(jxcCzrz1);
            logger.info("数据新增结束-销货"+jxcSale.toString());
            return new R().ok(jxcSaleService.save(jxcSale));
        }

    }
    /**
     * 修改数据
     *
     * @param jxcPici 实体对象
     * @return 修改结果
     */
    @PutMapping
    public R update(@RequestBody JxcPici jxcPici) {
        String onlyid = jxcPici.getOnlyid();
        String id = String.valueOf(jxcPici.getId());
        String orstate = jxcPici.getOrstate();
        String traner = jxcPici.getTraner().trim();
        String companyId = jxcPici.getCompanyId();
        //获取修改前的数据
        JxcPici oldjxcPici = jxcPiciService.getByOnlyid(onlyid);
        String newnumber = jxcPici.getNumber();
        String newunitPrice = jxcPici.getUnitPrice();
        if (orstate.equals("1")){
            //进货
            JxcStock jxcStock = jxcStockService.getById(id);
            JxcSupplier jxcSupplier = jxcSupplierService.getBy(traner, companyId);
            BigDecimal oldallamount = new BigDecimal(jxcStock.getAmount());
            BigDecimal oldSupplieramount = new BigDecimal(jxcSupplier.getAmount());
            BigDecimal oldamount = new BigDecimal(oldjxcPici.getAmount());
            BigDecimal newamount = new BigDecimal(newnumber).multiply(new BigDecimal(newunitPrice));
            logger.info("oldamount:"+oldamount);
            logger.info("newamount:"+newamount);
            if (newamount.compareTo(oldamount) > 0){
                //如果金额比原来多
                BigDecimal lisamount = newamount.subtract(oldamount);
                jxcStock.setAmount(String.valueOf(oldallamount.add(lisamount)));
//                jxcStock.setModel(jxcStock.getModel()+jxcPici.getModel());
                jxcStockService.updateById(jxcStock);

                JxcCzrz jxcCzrz1 = new JxcCzrz();
                jxcCzrz1.setId(DdygPiciController.setczrzid(id));
                jxcCzrz1.setCompanyId(companyId);
                jxcCzrz1.setInfor("金额:+"+String.valueOf(lisamount));
                jxcCzrz1.setType("进货单修改");
                R add = jxcCzrzService.add(jxcCzrz1);

                jxcSupplier.setAmount(String.valueOf(oldSupplieramount.add(lisamount)));
                jxcSupplierService.updateById(jxcSupplier);

                JxcCzrz jxcCzrz2 = new JxcCzrz();
                jxcCzrz2.setId(DdygPiciController.setczrzid(id));
                jxcCzrz2.setCompanyId(companyId);
                jxcCzrz2.setInfor("金额:+"+String.valueOf(lisamount));
                jxcCzrz2.setType("供应商修改");
                R add2 = jxcCzrzService.add(jxcCzrz2);
            }else if (newamount.compareTo(oldamount) < 0){
                //如果金额比原来少
                BigDecimal lisamount = oldamount.subtract(newamount);
                jxcStock.setAmount(String.valueOf(oldallamount.subtract(lisamount)));
//                jxcStock.setModel(jxcStock.getModel()+jxcPici.getModel());
                jxcStockService.updateById(jxcStock);

                JxcCzrz jxcCzrz1 = new JxcCzrz();
                jxcCzrz1.setId(DdygPiciController.setczrzid(id));
                jxcCzrz1.setCompanyId(companyId);
                jxcCzrz1.setInfor("金额:-"+String.valueOf(lisamount));
                jxcCzrz1.setType("进货单修改");
                R add = jxcCzrzService.add(jxcCzrz1);

                jxcSupplier.setAmount(String.valueOf(oldSupplieramount.subtract(lisamount)));
                jxcSupplierService.updateById(jxcSupplier);

                JxcCzrz jxcCzrz2 = new JxcCzrz();
                jxcCzrz2.setId(DdygPiciController.setczrzid(id));
                jxcCzrz2.setCompanyId(companyId);
                jxcCzrz2.setInfor("金额:-"+String.valueOf(lisamount));
                jxcCzrz2.setType("供应商修改");
                R add2 = jxcCzrzService.add(jxcCzrz2);
            }
            JxcCzrz jxcCzrz = new JxcCzrz();
            jxcCzrz.setId(DdygPiciController.setczrzid(id));
            jxcCzrz.setCompanyId(companyId);
            String infor = "金额:"+String.valueOf(oldamount)+"->"+String.valueOf(newamount);
            jxcCzrz.setType("进货单批次修改");

            if (!oldjxcPici.getModel().equals(jxcPici.getModel())) {
                jxcStock.setModel(jxcStock.getModel() + jxcPici.getModel());
                jxcStockService.updateById(jxcStock);
                infor += "型号->"+jxcPici.getModel();
            }
            jxcCzrz.setInfor(infor);
            R add2 = jxcCzrzService.add(jxcCzrz);
            return new R().ok(jxcPiciService.updateByOnlyid(jxcPici.getModel(),newnumber,newunitPrice, String.valueOf(newamount),onlyid));
        }else {
            //销售
            JxcSale jxcSale = jxcSaleService.getById(id);
            JxcCustom jxcCustom = jxcCustomService.getBy(traner, companyId);
            BigDecimal oldCustomamount = new BigDecimal(jxcCustom.getAmount());
            BigDecimal oldallamount = new BigDecimal(jxcSale.getAmount());
            BigDecimal oldamount = new BigDecimal(oldjxcPici.getAmount());
            BigDecimal newamount = new BigDecimal(newnumber).multiply(new BigDecimal(newunitPrice));
            if (newamount.compareTo(oldamount) > 0){
                //如果金额比原来多
                BigDecimal lisamount = newamount.subtract(oldamount);
                jxcSale.setAmount(String.valueOf(oldallamount.add(lisamount)));
//                jxcSale.setModel(jxcSale.getModel()+jxcPici.getModel());
                jxcSaleService.updateById(jxcSale);

                JxcCzrz jxcCzrz1 = new JxcCzrz();
                jxcCzrz1.setId(DdygPiciController.setczrzid(id));
                jxcCzrz1.setCompanyId(companyId);
                jxcCzrz1.setInfor("金额:+"+String.valueOf(lisamount));
                jxcCzrz1.setType("销货单修改");
                R add = jxcCzrzService.add(jxcCzrz1);

                jxcCustom.setAmount(String.valueOf(oldCustomamount.add(lisamount)));
                jxcCustomService.updateById(jxcCustom);

                JxcCzrz jxcCzrz2 = new JxcCzrz();
                jxcCzrz2.setId(DdygPiciController.setczrzid(id));
                jxcCzrz2.setCompanyId(companyId);
                jxcCzrz2.setInfor("金额:+"+String.valueOf(lisamount));
                jxcCzrz2.setType("客户修改");
                R add2 = jxcCzrzService.add(jxcCzrz2);
            }else if (newamount.compareTo(oldamount) < 0){
                //如果金额比原来少
                BigDecimal lisamount = oldamount.subtract(newamount);
                jxcSale.setAmount(String.valueOf(oldallamount.subtract(lisamount)));
//                jxcSale.setModel(jxcSale.getModel()+jxcPici.getModel());
                jxcSaleService.updateById(jxcSale);

                JxcCzrz jxcCzrz1 = new JxcCzrz();
                jxcCzrz1.setId(DdygPiciController.setczrzid(id));
                jxcCzrz1.setCompanyId(companyId);
                jxcCzrz1.setInfor("金额:-"+String.valueOf(lisamount));
                jxcCzrz1.setType("销货单修改");
                R add = jxcCzrzService.add(jxcCzrz1);

                jxcCustom.setAmount(String.valueOf(oldCustomamount.subtract(lisamount)));
                jxcCustomService.updateById(jxcCustom);

                JxcCzrz jxcCzrz2 = new JxcCzrz();
                jxcCzrz2.setId(DdygPiciController.setczrzid(id));
                jxcCzrz2.setCompanyId(companyId);
                jxcCzrz2.setInfor("金额:-"+String.valueOf(lisamount));
                jxcCzrz2.setType("客户修改");
                R add2 = jxcCzrzService.add(jxcCzrz2);
            }
            JxcCzrz jxcCzrz = new JxcCzrz();
            jxcCzrz.setCompanyId(companyId);
            String infor = "金额:"+String.valueOf(oldamount)+"->"+String.valueOf(newamount);
            jxcCzrz.setType("销货单批次修改");
            if (!oldjxcPici.getModel().equals(jxcPici.getModel())) {
                jxcSale.setModel(jxcSale.getModel()+jxcPici.getModel());
                jxcSaleService.updateById(jxcSale);
                infor += "型号->"+jxcPici.getModel();
            }
            jxcCzrz.setInfor(infor);
            R add2 = jxcCzrzService.add(jxcCzrz);
            return new R().ok(jxcPiciService.updateByOnlyid(jxcPici.getModel(),newnumber,newunitPrice, String.valueOf(newamount),onlyid));
        }
    }

    /**
     * 删除数据
     *
     * @param
     * @return 删除结果
     */
    @DeleteMapping
    public R delete(@RequestBody JSONObject params) {
        JSONArray idList = params.getJSONArray("idList");
        String orstate = params.getString("orstate");
        String companyId = params.getString("companyId");
        String onlyid = params.getString("onlyid");
        String traner = params.getString("traner").trim();
        //去除空格

        logger.info("批次删除数据"+params.toString());
        String id = idList.get(0).toString();
        String allamount = "";
        JxcPici jxcPici = jxcPiciService.getByOnlyid(onlyid);
        BigDecimal pcAmount = new BigDecimal(jxcPici.getAmount());

        JxcCzrz jxcCzrz = new JxcCzrz();
        jxcCzrz.setCompanyId(companyId);
        jxcCzrz.setInfor("批次删除：金额"+String.valueOf(pcAmount));

        if (orstate.equals("1")){
            jxcCzrz.setType("进货单批次删除");
            jxcCzrzService.add(jxcCzrz);
            //进货
            JxcStock jxcstock = jxcStockService.getallamount(id, companyId);
            JxcSupplier jxcSupplier = jxcSupplierService.getBy(traner, companyId);

            allamount = jxcstock.getAmount();
            BigDecimal bigDecimal = new BigDecimal(allamount);
            BigDecimal subtract = bigDecimal.subtract(pcAmount);
            String s = String.valueOf(subtract);
            if (s.equals("0.00")){
                jxcStockService.removeById(id);

                JxcCzrz jxcCzrz1 = new JxcCzrz();
                jxcCzrz1.setCompanyId(companyId);
                jxcCzrz1.setInfor("金额:"+String.valueOf(allamount));
                jxcCzrz1.setType("进货单删除");
                R add = jxcCzrzService.add(jxcCzrz1);
            }else {
                jxcstock.setAmount(s);
                jxcStockService.updateById(jxcstock);

                JxcCzrz jxcCzrz1 = new JxcCzrz();
                jxcCzrz1.setCompanyId(companyId);
                jxcCzrz1.setInfor("金额:"+String.valueOf(allamount)+"->"+s);
                jxcCzrz1.setType("进货单修改");
                R add = jxcCzrzService.add(jxcCzrz1);
            }
            if (jxcSupplier != null){
                BigDecimal oldSupplieramount = new BigDecimal(jxcSupplier.getAmount());
                jxcSupplier.setAmount(String.valueOf(oldSupplieramount.subtract(pcAmount)));
                jxcSupplierService.updateById(jxcSupplier);

                JxcCzrz jxcCzrz1 = new JxcCzrz();
                jxcCzrz1.setCompanyId(companyId);
                jxcCzrz1.setInfor("金额:"+String.valueOf(oldSupplieramount)+"->"+String.valueOf(oldSupplieramount.subtract(pcAmount)));
                jxcCzrz1.setType("供应商修改");
                R add = jxcCzrzService.add(jxcCzrz1);
            }
        }else {
            jxcCzrz.setType("销货单批次删除");
            jxcCzrzService.add(jxcCzrz);

            JxcSale jxcSale = jxcSaleService.getallamount(id, companyId);
            JxcCustom jxcCustom = jxcCustomService.getBy(traner, companyId);

            allamount = jxcSale.getAmount();
            BigDecimal bigDecimal = new BigDecimal(allamount);
            BigDecimal subtract = bigDecimal.subtract(pcAmount);
            String s = String.valueOf(subtract);
            if (s.equals("0.00")){
                jxcSaleService.removeById(id);

                JxcCzrz jxcCzrz1 = new JxcCzrz();
                jxcCzrz1.setCompanyId(companyId);
                jxcCzrz1.setInfor("金额:"+String.valueOf(allamount));
                jxcCzrz1.setType("销货单删除");
                R add = jxcCzrzService.add(jxcCzrz1);
            }else {
            jxcSale.setAmount(s);
            jxcSaleService.updateById(jxcSale);

            JxcCzrz jxcCzrz1 = new JxcCzrz();
            jxcCzrz1.setCompanyId(companyId);
            jxcCzrz1.setInfor("金额:"+String.valueOf(allamount)+"->"+s);
            jxcCzrz1.setType("销货单修改");
            R add = jxcCzrzService.add(jxcCzrz1);
            }
            if (jxcCustom != null){
            BigDecimal oldCustomamount = new BigDecimal(jxcCustom.getAmount());
            jxcCustom.setAmount(String.valueOf(oldCustomamount.subtract(pcAmount)));
            jxcCustomService.updateById(jxcCustom);

                JxcCzrz jxcCzrz1 = new JxcCzrz();
                jxcCzrz1.setCompanyId(companyId);
                jxcCzrz1.setInfor("金额:"+String.valueOf(oldCustomamount)+"->"+String.valueOf(oldCustomamount.subtract(pcAmount)));
                jxcCzrz1.setType("客户修改");
                R add = jxcCzrzService.add(jxcCzrz1);
            }
        }
        jxcPiciService.removeByOnlyid(onlyid);
        return new R().ok("删除成功");
    }

    public void Stockadd(JxcStock jxcStock,String formattedDateTime,String id){
        jxcStock.setId(id);
        jxcStock.setCreateTime(formattedDateTime);
        String seller = jxcStock.getSeller();
        String companyId = jxcStock.getCompanyId();
        String isPay = jxcStock.getIsPay();
//        String number = jxcStock.getNumber();
//        String unitPrice = jxcStock.getUnitPrice();
        //用bigdecimal计算总额，小数点位数为0
        String totalfee = String.valueOf(new BigDecimal(jxcStock.getAmount()));
        JxcSupplier jxcSupplier = jxcSupplierService.getBy(seller, companyId);
        if (jxcSupplier == null){
            JxcSupplier jxcSupplier1 = new JxcSupplier();
            jxcSupplier1.setId(id);
            jxcSupplier1.setCreateTime(formattedDateTime);
            jxcSupplier1.setName(seller);
            jxcSupplier1.setCompanyId(companyId);
            jxcSupplier1.setAmount(totalfee);
            jxcSupplier1.setIsPay(isPay);
            jxcSupplierService.save(jxcSupplier1);

            JxcCzrz jxcCzrz1 = new JxcCzrz();
            jxcCzrz1.setCompanyId(companyId);
            jxcCzrz1.setInfor("姓名:"+seller+"金额:"+totalfee);
            jxcCzrz1.setType("供应商新增");
            R add = jxcCzrzService.add(jxcCzrz1);
        }else {
            BigDecimal add1 = new BigDecimal(jxcSupplier.getAmount()).add(new BigDecimal(totalfee));
            jxcSupplier.setAmount(add1.toString());
            jxcSupplierService.updateById(jxcSupplier);

            JxcCzrz jxcCzrz1 = new JxcCzrz();
            jxcCzrz1.setCompanyId(companyId);
            jxcCzrz1.setInfor("姓名:"+seller+"金额+"+totalfee);
            jxcCzrz1.setType("供应商修改");
            R add = jxcCzrzService.add(jxcCzrz1);
        }
        jxcStock.setAmount(totalfee);
    }
    public void Saleadd(JxcSale jxcSale,String formattedDateTime,String id){
        jxcSale.setId(id);
        jxcSale.setCreateTime(formattedDateTime);
        String isPay = jxcSale.getIsPay();
        String buyer = jxcSale.getBuyer();
        String companyId = jxcSale.getCompanyId();
//        String number = jxcSale.getNumber();
//        String unitPrice = jxcSale.getUnitPrice();
        //用bigdecimal计算总额，小数点位数为0
        String totalfee = String.valueOf(new BigDecimal(jxcSale.getAmount()));
        JxcCustom jxcCustom = jxcCustomService.getBy(buyer, companyId);
        if (jxcCustom == null){
            JxcCustom jxcCustom1 = new JxcCustom();
            jxcCustom1.setId(id);
            jxcCustom1.setCreateTime(formattedDateTime);
            jxcCustom1.setName(buyer);
            jxcCustom1.setCompanyId(companyId);
            jxcCustom1.setAmount(totalfee);
            jxcCustom1.setIsPay(isPay);
            jxcCustomService.save(jxcCustom1);

            JxcCzrz jxcCzrz1 = new JxcCzrz();
            jxcCzrz1.setCompanyId(companyId);
            jxcCzrz1.setInfor("姓名:"+buyer+"金额:"+totalfee);
            jxcCzrz1.setType("供应商新增");
            R add = jxcCzrzService.add(jxcCzrz1);
        }else {
            BigDecimal add1 = new BigDecimal(jxcCustom.getAmount()).add(new BigDecimal(totalfee));
            jxcCustom.setAmount(add1.toString());
            jxcCustomService.updateById(jxcCustom);

            JxcCzrz jxcCzrz1 = new JxcCzrz();
            jxcCzrz1.setCompanyId(companyId);
            jxcCzrz1.setInfor("姓名:"+buyer+"金额+"+totalfee);
            jxcCzrz1.setType("客户修改");
            R add = jxcCzrzService.add(jxcCzrz1);
        }
        jxcSale.setAmount(totalfee);
    }
}

